#include "Geometry.fxh"

float g_DistScale;
float4x4 g_WorldView;
float4x4 g_WorldViewInvTranspose;

struct VS_TO_PS
{
	float4 Pos : POSITION;
	#if VERTEX_COLOR == 1
		float4 Color : COLOR0;
	#endif
	#ifdef USE_TEXCOORD
		float2 TexCoord : TEXCOORD0;
	#endif

	float Pos_View_z : TEXCOORD1;
};


void MainVS(in APP_TO_VS In, out VS_TO_PS Out)
{
	float3 pos_Local = PreprocessLocalPos(In);
	
	Out.Pos = mul(float4(pos_Local, 1), g_WorldViewProj);
	#if VERTEX_COLOR == 1
		Out.Color = In.Color;
	#endif
	#ifdef USE_TEXCOORD
		Out.TexCoord = CalcTexCoord(In);
	#endif

	Out.Pos_View_z = mul(float4(pos_Local, 1), g_WorldView).z;
}

void MainPS(
	in VS_TO_PS In,
	out float4 GBuffer0 : COLOR0,
	out float4 GBuffer1 : COLOR1)
{
	#if ALPHA_TEST != 0
		float alpha = 1;
		#if CONSTANT_COLOR != 0
			alpha *= g_ConstantColor.a;
		#endif
		#if VERTEX_COLOR != 0
			alpha *= In.Color.a;
		#endif
		#if TEXTURE != 0
			alpha *= tex2D(DiffuseSampler, In.TexCoord).a;
		#endif
		clip(alpha - g_AlphaTestRef);
	#endif

	GBuffer0.rgb = float3(0, 0, 0); // Color
	GBuffer0.a = 0; // Specular intensity
	GBuffer1.rg = float2(0, 0); // Normal
	GBuffer1.b = In.Pos_View_z * g_DistScale;
	GBuffer1.a = 0; // Specular exponent
}


technique Main {
	pass {
		VertexShader = compile vs_3_0 MainVS();
		PixelShader  = compile ps_3_0 MainPS();
	}
}

